package com.zhentao.controller;

import com.zhentao.pojo.Student;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

@Controller
public class FreemarkerController {


    @Autowired
    private Configuration configuration;

    @ResponseBody
    @RequestMapping("/createHtml")
    public String createHtml() throws IOException, TemplateException {

        Template template = configuration.getTemplate("02.ftl");

        List list = new ArrayList();
        list.add(new Student(1, "秦逸纪", 16, null));
        list.add(new Student(2, "榆木脑袋", 18, null));
        list.add(new Student(3, "李逸纪", 20, null));

        Map<String, Object> map = new HashMap<>();
        map.put("lists", list);
        template.process(map, new FileWriter("E://02.html"));


        return "OK";
    }





    @RequestMapping("/t1")
    public String t1(Model model) {
//        model.addAttribute("name", "秦逸纪");

        //对象
//        Student student = new Student();
//        student.setName("小明");
//        student.setAge(11);
//        student.setBirthDate("2015-11-12");
//        model.addAttribute("student", student);

        List list = new ArrayList();
        list.add(new Student(1, "秦逸纪", 16, new Date()));
        list.add(new Student(2, "榆木脑袋", 18, new Date()));
        list.add(new Student(3, "李逸纪", 20, new Date()));
        model.addAttribute("lists", list);
//        list.add(student);

        return "01-test";
    }


}
